package com.boyi.cycling_master.main.user;

import android.text.TextUtils;
import android.widget.TextView;

import com.boyi.cycling_master.application.CyclingApplication;
import com.boyi.cycling_master.bean.LoginResponseBean;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.PostParams;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.https.utils.UserInfoRetrofit;
import com.boyi.cycling_master.main.basemvp.BasePresenter;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @date 2017/7/28/028 14:15
 *
 * @author Lee_Mofeel
 *
 * @version 1.0
 *
 * @Description 个人中心管理器
 *
 */

public class PersonCenterPresenter extends BasePresenter<PersonCenterView> {


    /**
     * 修改用户信息
     * @param textView
     * @param key
     * @param value
     */
    public void changeUserInfo(final TextView textView, final String key, final String value){
        Map<String,String> map = new HashMap<>();
        map.put(key,value);
        switch (value){
            case "男":
                map.put(key,"0");
                break;
            case "女":
                map.put(key,"1");
                break;
        }

        UserInfoRetrofit.getInstance().changeUserInfo(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                if(response.body()==null){
                    view.showSnack("服务器异常");
                    return;
                }
                view.showSnack("修改成功");
                view.changeUserInfoSuccess(textView,value,key);
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {
                view.showSnack("服务器异常");
            }
        }, PostParams.makeRequestBody(map));
    }

    /**
     * 修改用户头像
     * @param imgPath
     */
    public void changeUserImg(String imgPath){
        Map<String,String> map = new HashMap<>();
        map.put("portrait",imgPath);
        UserInfoRetrofit.getInstance().changeUserHeadImg(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                if(response.body() == null)
                    return;
                if(response.body().getResultCode().equals("200")){
                    view.showSnack("修改成功");
                    String userImg = response.body().getDataCollection();
                    if(!TextUtils.isEmpty(userImg)){// 如果上传成功则刷新本地数据 并且跳转到下个界面
                        LoginResponseBean.DataCollectionBean userInfo =
                                CyclingApplication.getInstance().getUserInfo();
                        userInfo.setUserHead(userImg);
                        userInfo.save();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {
                view.showSnack(t.getMessage());
            }
        },PostParams.makeRequestBody(map));
    }

}
